package com.cy.controller;

import com.cy.bfzcommons.pojo.Product;
import com.cy.bfzcommons.service.ProductService;
import com.cy.bfzcommons.web.util.JsonResult;
import com.cy.bfzcommons.web.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/productVue")
@Api(
        value = "/productVue",description = "ProductControllerVue"
        ,tags = "商品模块(用于页面展现等)"
)
public class ProductControllerVue {
    @Autowired
    private ProductService productService;

    /**
     * 根据商品的id查询指定id
     * @param productId
     * @return
     */

    @GetMapping("{productId}")
    public JsonResult selectProductById(@ApiParam(name="productId",value="商品id",required=true)@PathVariable Integer productId){
        Product product = productService.VueProductById(productId);
        return JsonResult.ok(product);
    }

    /**
     * 查询所有商品
     * @return
     */

    @GetMapping("/findProduct/{state}")
    public JsonResult findProducts(@PathVariable Boolean state){
        return  JsonResult.ok( PageUtil.startpage().doSelectPageInfo(()->
                productService.vueGetProduct(state)));
    }

    /**
     * 根据分类查询商品
     * @param categoryId
     * @return
     */

    @GetMapping("/categorySelect/{categoryId}")
    public JsonResult categorySelect(@ApiParam(name="categoryId",value="商品分类id",required=true)@PathVariable Integer categoryId){
        List<Product> list = productService.categorySelect(categoryId);

        //无法使用分页, 原因  在业务层中多次查询 结果集是数据汇总起来的    导致mybatis的拦截器,拦截了最外层的请求  导致返回的数据和业务层的结果不一致
//        return JsonResult.success(PageUtil.startpage().doSelectPageInfo(()->
//                productService.categorySelect(categoryId)));
        return JsonResult.ok(list);
    }


    @GetMapping("/selectByUserId/{userId}/{state}")
    public JsonResult selectByUserId(@ApiParam(name="userId",value="用户id",required=true)@PathVariable Integer userId,
                                     @ApiParam(name="state",value="用户状态",required=true)@PathVariable Boolean state){
        List<Product> list =  productService.selectByUserId(userId,state);
        return JsonResult.ok(list);
    }

    @PostMapping("/insertProduct")
    public JsonResult insertProduct(@ApiParam(name="product",value="商品数据图片等",required=true)@RequestBody Product product){
        System.out.println(product.toString());
        productService.insertProduct(product);
        return JsonResult.ok();
    }


    @PutMapping("/updateProduct")
    public JsonResult updateProduct(@ApiParam(name="product",value="商品数据图片等",required=true)@RequestBody Product product){
        productService.VueUpdateProduct(product);
        return JsonResult.ok();
    }

    //根据条件查询商品
    @GetMapping("/selectLike")
    public JsonResult ss(String title,String category,Integer min,Integer max){
        System.out.println(min+" "+max);
        if("undefined".equals(title)) title = "";

        return JsonResult.ok(productService.productLike2(title,category,min,max));
    }



    @PutMapping("/updateStatus/{productId}/{state}")
    public JsonResult updateStates(@PathVariable Integer productId,@PathVariable Boolean state){
        Product product = new Product();
        product.setProductId(productId);
        product.setState(state);
        System.out.println(product);
        System.out.println("被执行了");
        //productService.updateProduct(product);
        //重写改状态
        productService.updateState(product);
        return JsonResult.ok();
    }

    @DeleteMapping("/deleteById/{productId}")
    public JsonResult deleteById(@PathVariable Integer productId){
        productService.delete(productId);
        return JsonResult.ok();
    }

}
